﻿using System;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.attend.personal
{
    public partial class report : ModulePage
    {
  
        private string GetWeekDay(DateTime date)
        {
            string str = string.Empty;
            str = date.ToString("yyyy-MM-dd");
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return (str + "(周日)");

                case DayOfWeek.Monday:
                    return (str + "(周一)");

                case DayOfWeek.Tuesday:
                    return (str + "(周二)");

                case DayOfWeek.Wednesday:
                    return (str + "(周三)");

                case DayOfWeek.Thursday:
                    return (str + "(周四)");

                case DayOfWeek.Friday:
                    return (str + "(周五)");

                case DayOfWeek.Saturday:
                    return (str + "(周六)");
            }
            return str;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.txtBeginDate.Value = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1";
                this.txtEndDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            }
            if (this.Page.IsPostBack)
            {
                int num8;
                DateTime time7;
                DateTime time8;
                DateTime time9;
                string str2;
                this.pnlQuery.Visible = false;
                this.pnlMain.Visible = true;
                DateTime time4 = Convert.ToDateTime(this.txtBeginDate.Value);
                DateTime time5 = Convert.ToDateTime(this.txtEndDate.Value);
                TimeSpan span = new TimeSpan(time4.Ticks);
                TimeSpan ts = new TimeSpan(time5.Ticks);
                int num3 = span.Subtract(ts).Duration().Days + 1;
                this.litNavTitle.Text = "[" + time4.ToString("D") + " 至 " + time5.ToString("D") + " 共" + num3.ToString() + "天]";
                int num4 = 30;
                int num5 = 10;
                int num6 = 10;
                int num7 = 60;
                AttendParaDao dao = new AttendParaDao();
                dao.GetPara();
                num4 = dao.DutyIntervalBefore1;
                num5 = dao.DutyIntervalAfter1;
                num6 = dao.DutyIntervalBefore2;
                num7 = dao.DutyIntervalAfter2;
                DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, HttpContext.Current.User.Identity.Name);
                string str = (userConfig.Rows[0]["General"] != DBNull.Value) ? userConfig.Rows[0]["General"].ToString() : "";
                if (str != string.Empty)
                {
                    str = "," + str + ",";
                }
                DataTable table2 = new DataTable();
                table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
                table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
                table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
                if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
                {
                    num8 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                    time7 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                    if (num8 == 1)
                    {
                        time8 = time7.AddMinutes((double)-num4);
                        time9 = time7.AddMinutes((double)num5);
                    }
                    else
                    {
                        time8 = time7.AddMinutes((double)-num6);
                        time9 = time7.AddMinutes((double)num7);
                    }
                    table2.Rows.Add(new object[] { num8, time7, time8, time9 });
                }
                DataTable table = new DataTable();
                table.Columns.Add("DutyDate", Type.GetType("System.DateTime"));
                for (int i = 1; i <= table2.Rows.Count; i++)
                {
                    table.Columns.Add("DutyDesc" + i.ToString(), Type.GetType("System.String"));
                }
                for (int j = 0; j < num3; j++)
                {
                    table.Rows.Add(new object[] { time4.AddDays((double)j) });
                    str2 = "SELECT RegisterType,RegisterTime,RegisterFlag,Remark FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + base.User.Identity.Name + "' AND [RegisterTime] >='" + time4.AddDays((double)j).ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + time4.AddDays((double)j).ToString("yyyy-MM-dd") + " 23:59:59'";
                    DataTable table4 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, str2).Tables[0];
                    for (int n = 0; n < table2.Rows.Count; n++)
                    {
                        DataRow[] rowArray = table4.Select("RegisterType = " + table2.Rows[n]["DutyType"].ToString() + " AND RegisterTime >='" + time4.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["BeginTime"].ToString()).ToString("HH:mm:ss") + "' AND RegisterTime <='" + time4.AddDays((double)j).ToString("yyyy-MM-dd") + " " + Convert.ToDateTime(table2.Rows[n]["EndTime"].ToString()).ToString("HH:mm:ss") + "'");
                        if (rowArray.Length > 0)
                        {
                            string str3 = Convert.ToDateTime(rowArray[0]["RegisterTime"].ToString()).ToString("HH:mm:ss");
                            if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 1)
                            {
                                str3 = str3 + " 迟到";
                            }
                            else if (Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString()) == 2)
                            {
                                str3 = str3 + " 早退";
                            }
                            if (rowArray[0]["Remark"] != DBNull.Value)
                            {
                                str3 = str3 + "<br>备注：" + rowArray[0]["Remark"].ToString();
                            }
                            table.Rows[j][n + 1] = str3;
                        }
                    }
                }
                StringBuilder builder = new StringBuilder();
                builder.Append("<table border=\"0\" cellspacing=\"1\" width=\"95%\" class=\"small\" bgcolor=\"#000000\" cellpadding=\"3\" align=\"center\">");
                builder.Append("<tr class=\"TableHeader\">");
                builder.Append("<td nowrap align=\"center\">日期</td>");
                for (int k = 0; k < table2.Rows.Count; k++)
                {
                    builder.Append(" <td nowrap align=\"center\">");
                    if (Convert.ToInt32(table2.Rows[k]["DutyType"].ToString()) == 1)
                    {
                        builder.Append("上班 ");
                    }
                    else
                    {
                        builder.Append("下班 ");
                    }
                    builder.Append("(" + Convert.ToDateTime(table2.Rows[k]["DutyTime"].ToString()).ToString("HH:mm:ss") + ")");
                    builder.Append(" </td>");
                }
                builder.Append("</tr>");
                DataView view = new DataView(table);
                string str4 = string.Empty;
                if (table.Columns.Count > 1)
                {
                    for (int num13 = 1; num13 < table.Columns.Count; num13++)
                    {
                        if (str4 == string.Empty)
                        {
                            str4 = str4 + "DutyDesc" + num13.ToString() + " IS NOT NULL";
                        }
                        else
                        {
                            str4 = str4 + " OR DutyDesc" + num13.ToString() + " IS NOT NULL";
                        }
                    }
                }
                if (str4 != string.Empty)
                {
                    view.RowFilter = str4;
                }
                for (int m = 0; m < view.Count; m++)
                {
                    bool flag;
                    int dayOfWeek = (int)Convert.ToDateTime(view[m]["DutyDate"].ToString()).DayOfWeek;
                    str2 = "SELECT [HolidayId] FROM [dbo].[Attend_Holiday] WHERE [BeginDate] <='" + Convert.ToDateTime(view[m]["DutyDate"].ToString()).ToString("yyyy-MM-dd") + "' AND '" + Convert.ToDateTime(view[m]["DutyDate"].ToString()).ToString("yyyy-MM-dd") + "' <= [EndDate]";
                    if (SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, str2) != null)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                    if (flag)
                    {
                        builder.Append("<tr class=\"TableContent\">");
                    }
                    else if (str.IndexOf("," + dayOfWeek.ToString() + ",") >= 0)
                    {
                        builder.Append("<tr class=\"TableContent\">");
                    }
                    else
                    {
                        builder.Append("<tr class=\"TableData\">");
                    }
                    builder.Append("<td nowrap align=\"center\">" + this.GetWeekDay(Convert.ToDateTime(view[m]["DutyDate"].ToString())) + "</td>");
                    for (int num16 = 1; num16 <= table2.Rows.Count; num16++)
                    {
                        builder.Append("<td align=\"center\">");
                        if (view[m][num16] == DBNull.Value)
                        {
                            if (flag)
                            {
                                builder.Append("节假日");
                            }
                            else if (str.IndexOf("," + dayOfWeek.ToString() + ",") >= 0)
                            {
                                builder.Append("公休日");
                            }
                            else
                            {
                                builder.Append("未登记");
                            }
                        }
                        else
                        {
                            builder.Append(view[m][num16].ToString());
                        }
                        builder.Append("</td>");
                    }
                    builder.Append("</tr>");
                }
                builder.Append("</table>");
                if (view.Count == 0)
                {
                    this.litDuty.Text = base.ShowMessage("无上下班登记记录");
                }
                else
                {
                    this.litDuty.Text = builder.ToString();
                }
            }
        }

    }
}
